package com.bosssoft.trainee.nontax.config;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author MaYunFei
 * @ProjectName RBAC_BOSS
 * @Description TODO
 * @time 2022/6/26 11:05
 */
@Configuration
@Aspect
public class AspectJ {

    final Logger logger = LoggerFactory.getLogger(AspectJ.class);

    @Pointcut("execution(* com.bosssoft.trainee.nontax.controller.*.*(..))")
    public void point() {}

    /**
     * 前置通知
     * 连接点之前执行
     */
    @Before("point()")
    public void before(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.info("start methodName:" + methodName + ", args:" + Arrays.toString(args));
    }

    /*@Around("point()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.info("around() before proceed");
        Object proceed = joinPoint.proceed();
        logger.info("around() after proceed methodName:" + methodName + ", args:" + Arrays.toString(args) + ", result:" + proceed);
        return proceed;
    }*/

    /**
     * 后置通知
     * 连接点方法完成后执行
     * 无论连接点执行成功与否该通知都会执行
     */
    @After("point()")
    public void after(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.info("finish methodName:" + methodName + ", args:" + Arrays.toString(args));
    }

    /**
     * 连接点方法执行成功后执行
     * 可以拿到返回结果
     */
    @AfterReturning(value = "point()", returning = "result")
    public void result(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        logger.info("AfterReturning methodName:" + methodName + ", args:" + Arrays.toString(args) + ", result:" + result);
    }

    /**
     * 连接点方法执行异常后执行
     * 可以拿到异常信息
     */
    @AfterThrowing(value = "point()", throwing = "exception")
    public void afterThrowing(JoinPoint joinPoint, Exception exception) {
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //System.out.println("afterThrowing() methodName:" + methodName + ", args:" + Arrays.toString(args) + ", exception:" + exception);
        logger.error("afterThrowing methodName:" + methodName + ", args:" + Arrays.toString(args) + ", exception:" + exception);
    }

}
